1 package edu.jiangxin.apktoolbox.file.zhconvert;
2
3 import org.apache.logging.log4j.LogManager;
4 import org.apache.logging.log4j.Logger;
5
6 import java.io.*;
7 import java.util.Properties;
8
9 public class ZHConverterUtils {
10 private static final Logger logger = LogManager.getLogger(ZHConverterUtils.class.getSimpleName());
11
12 private Properties charMap = new Properties();
13
14 private Properties charMap2 = new Properties();
15
16 public ZHConverterUtils() {
17 String dirpath = "zhSimple2zhTw.properties";
18 String dirpath2 = "zhTw2zhSimple.properties";
19 initProperties(dirpath,charMap);
20 initProperties(dirpath2,charMap2);
21 }
22
23 private void initProperties(String rPath, Properties properties) {
24 File file = new File(rPath);
25 if (!file.exists()) {
26 try {
27 file.createNewFile();
28 } catch (IOException e) {
29 logger.error("createNewFile failed: " + e.getMessage());
30 return;
31 }
32 }
33
34 try (InputStream is = new FileInputStream(file);
35 BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
36 properties.load(reader);
37 } catch (IOException e) {
38 logger.error("load failed: " + e.getMessage());
39 }
40 }
41
42
43
44
45
46
47
48 public String myConvertToTW(String str){
49 for (Object o : charMap.keySet()) {
50 String key = (String) o;
51 if (!key.isEmpty() && str.contains(key)) {
52 str = str.replaceAll(key, charMap.getProperty(key));
53 }
54 }
55 return str;
56 }
57
58
59
60
61
62
63 public String myConvertToSimple(String str){
64 for (Object o : charMap2.keySet()) {
65 String key = (String) o;
66 if (!key.isEmpty() && str.contains(key)) {
67 str = str.replaceAll(key, charMap2.getProperty(key));
68 }
69 }
70 return str;
71 }
72
73
74
75
76
77
78
79 public void storeDataToProperties(String key,String value) {
80 charMap.setProperty(key,value);
81 String filePath = "zhSimple2zhTw.properties";
82 try (OutputStream out = new FileOutputStream(filePath)) {
83 charMap.store(out, "加入新元素");
84 } catch (IOException e) {
85 logger.error("storeDataToProperties failed: IOException");
86 }
87 charMap2.setProperty(value,key);
88 String filePath2 = "zhTw2zhSimple.properties";
89 try (OutputStream out2 = new FileOutputStream(filePath2)) {
90 charMap2.store(out2, "加入新元素");
91 } catch (IOException e) {
92 logger.error("storeDataToProperties failed: IOException");
93 }
94 }
95
96 public Properties getCharMap() {
97 return charMap;
98 }
99
100
101
102 }